package com.sunducation.wts.penbox;

import android.Manifest;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.contract.EmptyCon;
import com.wetao.lib_common.presenter.EmptyPre;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.ComUtil;
import com.wetao.lib_common.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ConstantArouter.PATH_APP_MAINACTIVITY)
public class MainActivity extends BaseActivity<EmptyCon.View, EmptyPre>
        implements EmptyCon.View{
    private final static String TAG = "MainActivity";

    @BindView(R2.id.btn_demoCyh)
    Button btn_demoCyh;


    @Override
    public int getLayoutId() {
        //return R.layout.gather_activity_main;
        return R.layout.activity_main;
    }

    @Override
    public EmptyPre createPresenter() {
        return new EmptyPre(this);
    }

    @Override
    public EmptyCon.View createView() {
        return this;
    }

    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }

    @Override
    public void init() {
        permission();
        initData();
        initView();
    }
    public void initData() {
        String imei = ComUtil.getIMEI(this);

        String imei1 = ComUtil.getIMEI(this,0);
        String imei2 = ComUtil.getIMEI(this,1);

        LogUtils.i("m1:imei=" + imei);
        LogUtils.i("m1:imei=" + imei1);
        LogUtils.i("m1:imei=" + imei2);

        DisplayMetrics dm = new DisplayMetrics();

        dm = getResources().getDisplayMetrics();

        float density = dm.density; // 屏幕密度(像素比例：0.75/1.0/1.5/2.0)

        int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素：120/160/240/320)

        float xdpi = dm.xdpi;

        float ydpi = dm.ydpi;

        LogUtils.e(TAG + " DisplayMetrics: " +  "xdpi=" + xdpi + "; ydpi=" + ydpi);

        LogUtils.e(" DisplayMetrics：" + "density=" + density + "; densityDPI=" + densityDPI);

        int screenWidth = dm.widthPixels; // 屏幕宽(像素，如：480px)

        int screenHeight = dm.heightPixels; // 屏幕高(像素，如：800px)

        LogUtils.e(" DisplayMetrics(111): " +  "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);



        int screenWidth2 = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素，如：480px)

        int screenHeight2 = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素，如：800p)

        LogUtils.e(" getDefaultDisplay ：：" +  "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

        //SPUtils.getInstance().put(Constants.USER_ID_KEY, "18823185102");
    }

    public void initView() {
        btn_demoCyh.setText("wo");
    }

    @OnClick({R2.id.btn_demoCyh,
            R2.id.btn_login,
            R2.id.btn_pen,
            R2.id.btn_kxxl
    })
    void onClickView(View v) {
        if(v.getId() == R.id.btn_demoCyh) {
            //ARouter.getInstance().build(ConstantArouter.PATH_DEMOCYH_CYHMAINACTIVITY).navigation();
            ARouter.getInstance().build(ConstantArouter.PATH_DEMOCYH_SMARTACTIVITY).navigation();
        } else if(v.getId() == R.id.btn_login) {
            ARouter.getInstance().build(ConstantArouter.PATH_COMMON_LOGINACTIVITY).navigation();
        } else if(v.getId() == R.id.btn_pen) {
            ARouter.getInstance().build(ConstantArouter.PATH_PEN_PENMAINACTIVITY).navigation();
        } else if(v.getId() == R.id.btn_kxxl) {
            ARouter.getInstance().build(ConstantArouter.PATH_HOMEWORK_MAINACTIVITY).navigation();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        LogUtils.i("m1:--onKeyDown keyCode = " + keyCode);
        if(keyCode == KeyEvent.KEYCODE_F11){

            LogUtils.i("m1:跳转Wi-Fi");
            /*Intent in=new Intent();
            in.setClass(this, WifiSettingActivity.class);
            startActivityForResult(in, Constants.ConstData.REQUSTCODE_WIFI_SET);*/

            ARouter.getInstance().build(ConstantArouter.PATH_COMMON_WIFISETTINGACTIVITY)
                    .navigation();
            return true;
        } else if(keyCode == KeyEvent.KEYCODE_1) {

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private static final int PERMISSION_REQUEST_CODE = 102;
    private void permission(){
        List<String> permissionLists = new ArrayList<>();
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.RECORD_AUDIO);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.ACCESS_NETWORK_STATE);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
            permissionLists.add(Manifest.permission.READ_PHONE_STATE);
        }

        if(!permissionLists.isEmpty()){//说明肯定有拒绝的权限
            ActivityCompat.requestPermissions(this, permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUEST_CODE);
        }else{
            //gotoMainAct();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if(grantResults.length > 0){
                    for(int grantResult : grantResults){
                        if(grantResult != PackageManager.PERMISSION_GRANTED){
                            finish();
                            return;
                        }
                    }
                    //其他逻辑(这里当权限都同意的话就执行下一步逻辑)
                    //gotoMainAct();
                }
                break;
            default:
                break;
        }
    }


}
